/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.Userexercisesetlog;
import me.zhengjie.modules.system.domain.vo.UserexercisesetlogQueryCriteria;
import me.zhengjie.modules.system.mapper.UserMapper;
import me.zhengjie.modules.system.mapper.UserexercisesetlogMapper;
import me.zhengjie.modules.system.service.UserexercisesetlogService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author shitou
* @date 2024-05-29
**/
@Service
@RequiredArgsConstructor
public class UserexercisesetlogServiceImpl extends ServiceImpl<UserexercisesetlogMapper, Userexercisesetlog> implements UserexercisesetlogService {

    private final UserexercisesetlogMapper userexercisesetlogMapper;

    private final UserMapper userMapper;

    @Override
    public PageResult<Userexercisesetlog> queryAll(UserexercisesetlogQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(userexercisesetlogMapper.findAll(criteria, page));
    }

    @Override
    public List<Userexercisesetlog> queryAll(UserexercisesetlogQueryCriteria criteria){
        return userexercisesetlogMapper.findAll(criteria);
    }
    public Userexercisesetlog getOneMsg(QueryWrapper queryWrapper){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);
        if(currentUser.getId()!=null){
            queryWrapper.eq("userId",currentUser.getId());
        }

        return userexercisesetlogMapper.selectOne(queryWrapper);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Userexercisesetlog resources) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);

        resources.setUserid(currentUser.getId());
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Userexercisesetlog resources) {

        Userexercisesetlog userexercisesetlog = getById(resources.getId());
        userexercisesetlog.copy(resources);
        saveOrUpdate(userexercisesetlog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Userexercisesetlog> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Userexercisesetlog userexercisesetlog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" type",  userexercisesetlog.getType());
            map.put(" teachid",  userexercisesetlog.getTeachid());
            map.put(" teachchapterid",  userexercisesetlog.getTeachchapterid());
            map.put(" teachsubsectionid",  userexercisesetlog.getTeachsubsectionid());
            map.put(" subsectionexercisesetid",  userexercisesetlog.getSubsectionexercisesetid());
            map.put(" courseid",  userexercisesetlog.getCourseid());
            map.put(" exercisesetid",  userexercisesetlog.getExercisesetid());
            map.put(" userid",  userexercisesetlog.getUserid());
            map.put(" iscomplete",  userexercisesetlog.getIscomplete());
            map.put(" createtime",  userexercisesetlog.getCreatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}